#include <libdswsession/dswlanmsgsession.h>

CDswlanmsgsession::CDswlanmsgsession(LPST_ENV_T pEnv, struct event_base * base, int nfd, char * pKey)
: CDswmsgsession_base(pEnv, base, nfd, LANMSG_SESSION_HEADSIZE)
{
	CMarkup xml;
	
	LOG4CPLUS_DEBUG(log_dsw, "Create CDswlanmsgsession class! session [ " << pKey << " ]");
	strcpy(m_sKey, pKey);
	MakeReportPUAV2(xml);
	SendMsg((char*)xml.GetDoc().c_str());
}

CDswlanmsgsession::~CDswlanmsgsession()
{
	LOG4CPLUS_DEBUG(log_dsw, "Destroy CDswlanmsgsession class! session [ " << m_sKey << " ]");
}

void CDswlanmsgsession::SendMsg(char * pMsg)
{
	int nSendLen = strlen(pMsg);
	if (nSendLen > 0){
		evbuffer_add(bufferevent_get_output(m_pBev), &nSendLen, sizeof(int));
		evbuffer_add(bufferevent_get_output(m_pBev), pMsg, nSendLen);
	}
}

void CDswlanmsgsession::OnError(struct bufferevent *bev, short events)
{
	delete this;
}

void CDswlanmsgsession::MakeReportPUAV2(CMarkup & xml)
{
	char sbuf[256];
	ST_DEVICE_BASE_T nParam;
	ST_GPRS_ALARM_PARAM_T nAlarmParam;
	int nAVPort = 0;
	int nMsgPort = 0;
	sockaddr_in addrMy;
	int len = sizeof(addrMy);
	ST_CURIP_MAC_T nCurIpMac;
	ST_VIDEO_PARAM nVParam;
	ST_EXTERNAL_CAM_EX_T nExtcams[MAX_CHANNEL_REMOTE];
	int nCh;

	SendMessage(PMSG_GET_VIDEO_PARAM, &nVParam);
	memset(nExtcams, 0, sizeof(nExtcams));
	SendMessage(PMSG_GET_EXTCAMS, nExtcams);

	strcpy(nCurIpMac.sIp, "0.0.0.0");
	strcpy(nCurIpMac.sMac, "00:00:00:00:00:00");
	memset(&addrMy,0,sizeof(addrMy));
	if (getsockname(bufferevent_getfd(m_pBev), (sockaddr*)&addrMy, (socklen_t *)&len) == 0)
		strcpy(nCurIpMac.sIp, inet_ntoa(addrMy.sin_addr));
	SendMessage(PMSG_GET_CURIPMAC, (void*)&nCurIpMac);
	SendMessage(PMSG_GET_DEVICE_BASE_PARAM, (void*)&nParam);
	sprintf(sbuf, "%s_%d", nParam.devid, time(NULL));
	MAKEMSGHEAD_V2(xml, sbuf, "MSG_PUA_REGIST_V2");
	xml.IntoElem();
		xml.AddElem("DSW_RESULT");
		xml.SetAttrib("ERRCODE", 0);
		xml.IntoElem();
			xml.AddElem("DSW_DEV");
			xml.SetAttrib("PUAID", nParam.devid);
			xml.SetAttrib("PUATYPE", nParam.type);
			xml.SetAttrib("PUAIP", nCurIpMac.sIp);
			xml.SetAttrib("PUAMAC", nCurIpMac.sMac);
			xml.SetAttrib("WEBPORT", 80);
			memset(&nAlarmParam, 0, sizeof(nAlarmParam));
			SendMessage(PMSG_GET_GPRS_ALARM_PARAM, (void*)&nAlarmParam);
			xml.SetAttrib("PUALOCK", nAlarmParam.nLockType);
			SendMessage(PMSG_GET_OSVER, (void*)sbuf);
			xml.SetAttrib("OSVER", sbuf);
			SendMessage(PMSG_GET_APPVER, (void*)sbuf);
			xml.SetAttrib("APPVER", sbuf);
			SendMessage(PMSG_GET_VIDEOLISTEN_PORT, (void*)&nAVPort);
			xml.SetAttrib("AVPORT", nAVPort);
			SendMessage(PMSG_GET_MSGLISTEN_PORT, (void*)&nMsgPort);
			xml.SetAttrib("MSGPORT", nMsgPort);
			SendMessage(PMSG_GET_LOCATION, (void*)sbuf);
			xml.SetAttrib("LOCATE", sbuf);
			xml.SetAttrib("CHS", m_pEnv->nMaxChannel_Local);
			xml.SetAttrib("CHS_R", m_pEnv->nMaxChannel_Remote);

			xml.IntoElem();
				for (nCh = 0; nCh < m_pEnv->nMaxChannel_Local; nCh++){
					xml.AddElem("LOCAL");
					xml.AddAttrib("CH", nVParam.nVParam[nCh].ch_num);
					xml.AddAttrib("NAME", nVParam.nVParam[nCh].nOsd.sosd);
				}
				for (nCh = 0; nCh < m_pEnv->nMaxChannel_Remote; nCh++){
					if (nExtcams[nCh].nType == EN_EXTCAM_UNKNOW)
						continue;
					xml.AddElem("REMOTE");
					xml.AddAttrib("CH", nExtcams[nCh].nChId);
					xml.AddAttrib("TYPE", nExtcams[nCh].nType);
					xml.AddAttrib("NAME", nExtcams[nCh].sName);
					xml.AddAttrib("EN",  nExtcams[nCh].bEnabled);
					SendMessage(PMSG_GET_EXTCAM_LIVESEC, &nExtcams[nCh]);
					xml.AddAttrib("ONLINE", IS_ONLINE(nExtcams[nCh].nLiveSec));
					xml.IntoElem();
						if (nExtcams[nCh].nCapbility & EXTCAM_CAPBILITY_MAINSTREAM){
							xml.AddElem("STREAM");
							xml.AddAttrib("ID", MAIN_STREAM);
							xml.AddAttrib("URL", nExtcams[nCh].sStreamURL[MAIN_STREAM]);
						}
						if (nExtcams[nCh].nCapbility & EXTCAM_CAPBILITY_SUBSTREAM){
							xml.AddElem("STREAM");
							xml.AddAttrib("ID", SUB_STREAM);
							xml.AddAttrib("URL", nExtcams[nCh].sStreamURL[SUB_STREAM]);
						}
					xml.OutOfElem();
				}
			xml.OutOfElem();

		xml.OutOfElem();
	xml.OutOfElem();
}

